package com.spring.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.spring.pojo.ApiResponse;
import com.spring.pojo.Movie;

import java.io.IOException;
import java.net.URL;
import java.util.List;

public class ApiUtil {
    private static final String TMDB_BASE_URL = "https://api.themoviedb.org/3";
    private static final String API_KEY = "1fa7c99b03760ee82a1b6065df8485a0";

    private static final ObjectMapper mapper = new ObjectMapper();

    public static List<Movie> getPopularMovies() throws IOException {
        URL url = new URL(TMDB_BASE_URL + "/movie/popular?api_key=" + API_KEY);
        String json = NetUtil.readUrl(url);
        ApiResponse response = mapper.readValue(json, ApiResponse.class);
        return response.getResults();
    }
}
